<?php

namespace App\Jobs\QiPao;

use App\Jobs\Job;
use App\Jobs\QiPao\Gift\ActivityJob;
use App\Jobs\QiPao\Gift\Gift520Job;
use App\Jobs\QiPao\Gift\GiftDatingJob;
use App\Jobs\QiPao\Gift\GiftExpJob;
use App\Jobs\QiPao\Gift\NewConnectionJob;
use App\Jobs\QiPao\Gift\RankJob;
use App\Jobs\QiPao\Gift\GiftWallJob;
use App\Jobs\QiPao\Gift\SociatyMasterJob;
use App\Jobs\QiPao\Gift\WeekStarJob;
use App\Jobs\QiPao\Gift\ConnectionJob;
use App\Jobs\QiPao\Gift\GiftWallRankJob;
use App\Jobs\QiPao\Gift\SociatyIncomeJob;
use App\Jobs\RoomPK\PKGiftJob;
use App\Jobs\Streamer\Task\ReceiveGiftsJob;
use App\Models\SociatyDB\SociatyUser;
use App\Services\Accompany\AccompanyOrderService;
use App\Services\Tools\IdempotentConsume;
use App\Services\Tools\RedisService;
use Illuminate\Support\Facades\Log;
use App\Jobs\QiPao\Gift\RoomContributionJob;

class GiftJob extends Job
{
    protected $ids;//ln_give_gift表的主键id数组
    const jobName = 'GiftJob';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($ids)
    {
        $this->ids = $ids;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(env('APP_ENV') != 'production'){
            Log::error('giftJob:' . json_encode(['time' => getMillisecond(), 'data' => $this->ids]));
        }
        if (is_array($this->ids)) {
            $ids = $this->ids;
            $idempotentConsumeServer =  new IdempotentConsume();
            //删除es项目的对应zset的值，防止定时重复投递
            $idempotentConsumeServer->removeEasySwooleGiftJob(self::jobName,$ids);

            $give_gift_list = app('model_give_gift')->whereIn('id', $ids)->get();
            Log::error('giftJob:give_gift_list:'.json_encode(
                    [
                        'time' => getMillisecond(),
                        'data' => $give_gift_list->toArray(),
                        'ids' =>  $this->ids,
                    ]
                ));
            // 获取房间类型

            if (!$give_gift_list->isEmpty()) {
                $give_gift_list = $give_gift_list->toArray();
                //幂等消费类
                foreach ($give_gift_list as $k=> $data) {
                    if($idempotentConsumeServer->checkIdempotentRedisKey(self::jobName,$data['id'])){
                        // 相亲房
                        $roomType = RedisService::getDatingRoomList($data['room_id']);
                        if($roomType){
                            dispatch(new GiftDatingJob($data));
                        }
                        dispatch(new GiftWallJob($data));
//                        dispatch(new GiftWallRankJob($data));//任务不存在导致SociatyMasterJob消息丢失这里关掉

                        //非 派单房收礼机的送礼 才生成榜单
                        if (empty($data['room_demand_id'])) {
                            dispatch(new RankJob($data));
                        }
                        dispatch(new SociatyIncomeJob($data));
                        dispatch(new WeekStarJob($data));
                        dispatch(new RoomContributionJob($data));
                        dispatch(new NewConnectionJob($data));
//                         dispatch(new Gift520Job($data));
                        // dispatch(new PKGiftJob($data));
                        dispatch(new ReceiveGiftsJob($data["gift_user"], $data["total_price"]));
                        // 开启活动消费
                        dispatch(new ActivityJob($data));
                        //派单推送 - 大神接单
                        if ($data['room_demand_id']) {
                            (new AccompanyOrderService())->sendDemandGift($data['room_demand_id']);
                        }
                    }else{
                        unset($give_gift_list[$k]);
                    }
                }
                if(!empty($give_gift_list)){
                    //送礼物经验值  和魅力升级
//                    dispatch(new GiftExpJob($give_gift_list));
                    dispatch(new GiftExpJob($give_gift_list))->onQueue("GiftExpJob");
                }
            }
        }
    }

    /* public function fail($exception = null )
     {
         Log::error('giftJob:'.$exception->getMessage());
     }*/
}
